<?php
#### NOTICE ####################################################################
// Used to display notices like confirmations or errors
function notice($message, $type = 'success', $back_or_url_array = FALSE) {
	$base =& Base::getInstance();

	// Display link to return to previous / redirect page
	$message .= build_links($back_or_url_array);

	if ($type == 'success') { $class = 'success notice'; $icon = $base->icon['success']; }
	else if ($type == 'info') { $class = 'info notice'; $icon = $base->icon['info']; }
	else if ($type == 'warning') { $class = 'warning notice'; $icon = $base->icon['warning']; }
	else { $class = 'error notice'; $icon = $base->icon['error']; }

	$javascript = '<script type="text/javascript">
		$(document).ready(function() {
			$("#notice").hide().fadeIn("slow");
		});
	</script>';

	return $javascript.'<div id="notice" class="'.$class.'">'.$icon.' '.$message.'</div>';
}

#### MESSAGE ###################################################################
// Used to display messages like confirmations or errors
function message($title, $message, $back_or_url_array = FALSE, $force_quit = FALSE) {

	// Display link to return to previous / redirect page
	$message .= '<br /><br /><center>'.build_links($back_or_url_array).'</center>';

	if ($title) {
		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $message);
		$output = $template->output();
	}
	else $output = $message;

	// If force_quit is TRUE, only the message will be output and code flow ended
	if ($force_quit == TRUE) {
		echo $output;
		die();
	}
	else return $output;
}

#### BUILD LINKS ###############################################################
function build_links($back_or_url_array = FALSE) {
	$base =& Base::getInstance();

	if (!$back_or_url_array) return;

	// Display link to return to previous / redirect page
	if (is_array($back_or_url_array)) {
		$message = NULL;
		foreach ($back_or_url_array as $url) {
			$title_attrib = (isset($url[2]) ? ' title="'.$url[2].'"' : NULL);
			$links[] = '<a href="'.$url[0].'"'.$title_attrib.'><b>'.$url[1].'</b></a>';
		}
		$message .=	implode(' '.$base->icon['separator'].' ', $links);
	}
	else if ($back_or_url_array == TRUE) {
		$message = '<a href="javascript:history.back(1)"><b>Back</b></a>';
	}

	return ' '.$message;
}

#### SQL ERROR #################################################################
// Handles SQL error messages
function sql_error($file = 'Unknown', $line = 'Unknown') {
	$base =& Base::getInstance();

	$sql_error_number = $base->db->getErrorNumber();
	$sql_error_message = $base->db->getErrorMessage();
	$last_query = htmlspecialchars($base->db->last_query);

	$title = 'SQL Error';
	$content = 'You have encountered an SQL error.<br />';
	if (permission('sql_errors')) {
		$content .= '<br /><b>File:</b> '.$file.'<br />';
		$content .= '<b>Line:</b> '.$line.'<br />';
		$content .= '<b>SQL Error:</b> ('.$sql_error_number.') '.$sql_error_message.'<br />';
		$content .= '<b>Query:</b> '.$last_query.'<br />';
	}

	if ($base->config['report_sql_errors']) {
		$recipient = $base->config['admin_email'];
		$subject = $base->config['name'].' - SQL Error';

		$body = 'Username: '.(isset($_SESSION['lf']['username']) ? $_SESSION['lf']['username'] : 'Anonymous')."\r\n";
		$body .= 'Users IP: '.(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'Unknown')."\r\n";
		$body .= 'Script: '.(isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : 'Unknown')."\r\n";
		$body .= 'Path Info'.(isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : 'Unknown')."\r\n";
		$body .= 'Query string: '.(isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : 'Unknown')."\r\n";
		$body .= "File: ".$file."\r\n";
		$body .= "Line: ".$line."\r\n";
		$body .= "SQL Error: (".$mysql_errno.") ".$mysql_error."\r\n";
		$body .= "Query: ".$last_query."\r\n";

		$headers = "From: ".$base->config['name']." Auto-Mailer <".$base->config['admin_email'].">\r\n";
		$base->load->helper('email');
		email($recipient, $subject, $body, $headers);

		$content .= '<br />The sites\'s administrator has been notified';
	}

	echo box($title, $content);

}

#### REPORT ####################################################################
// Report something to the site management via email and optional display to
// the user
function report($report_string, $display_message = TRUE) {
	$base =& Base::getInstance();

	$title = 'Attention!';
	$recipient = $base->config['admin_email'];
	$subject = $base->config['name'].' - '.$report;

	$body = 'Username: '.(isset($_SESSION['lf']['username']) ? $_SESSION['lf']['username'] : 'Anonymous')."\r\n";
	$body .= 'Users IP: '.(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'Unknown')."\r\n";
	$body .= 'Script: '.(isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : 'Unknown')."\r\n";
	$body .= 'Path Info'.(isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : 'Unknown')."\r\n";
	$body .= 'Query string: '.(isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : 'Unknown')."\r\n";
	$body .= "\r\n";
	$body .= 'Report: '.$report_string."\r\n";

	$headers = "From: ".$base->config['name']." Auto-Mailer <".$base->config['admin_email'].">\r\n";
	$base->load->helper('email');
	email($recipient, $subject, $body, $headers);

	if ($message) {
		$template = new template('box', 'common');
		$template->assign('TITLE', $title);
		$template->assign('BODY', $report_string.'<br />The site managment have been notified');
		echo $template->output();
		die();
	}
}
?>
